package com.ytc.system.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ytc.commons.constants.CommonConstants;
import com.ytc.commons.enums.ErrorCode;
import com.ytc.commons.model.dto.ApproveDto;
import com.ytc.commons.model.dto.CompanyDto;
import com.ytc.commons.model.dto.PageRequest;

import com.ytc.commons.model.vo.CompanyVo;
import com.ytc.commons.model.vo.PageResponseResult;
import com.ytc.commons.model.vo.ResponseResult;

import com.ytc.commons.utils.BeanHelper;
import com.ytc.system.domain.Company;
import com.ytc.system.domain.User;
import com.ytc.system.mappers.CompanyMapper;
import com.ytc.system.mappers.UserMapper;
import com.ytc.system.service.CompanyService;
import com.ytc.system.service.UserService;
import com.ytc.web.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    //查询企业集合
    @Override
    public ResponseResult listCompany(PageRequest request, CompanyDto dto) {
        //创建分页参数
        Page<Company> pageParam = new Page<>(request.getPageNo(),request.getPageSize());
        //查询条件
        String[] split = dto.getApprovalStatus().split(",");
        LambdaQueryWrapper<Company> qw =new LambdaQueryWrapper<>();
        if(split.length==1){
            qw.eq(Company::getApprovalStatus,split[0]);
        }else{
            qw.in(Company::getApprovalStatus,split[0],split[1]);
        }

        //分页查询
        Page<Company> page =this.page(pageParam, qw);//分页起始条数Page，查询条件QueryWarrper
        Long total =  this.count(qw);
        page.setTotal(total);
        //转化PageResponseResult
        return new PageResponseResult(page.getTotal(), page.getRecords());
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    //机构入驻
    @Override
    public void savedto(CompanyDto companyDto) {
        String o = (String)redisTemplate.opsForValue().get("CODE_" + companyDto.getPhone());
        if (StringUtils.isNotEmpty(o)){
            if (o.equals(companyDto.getVerifyCode())){
                Company company = BeanHelper.copyProperties(companyDto, Company.class);
                company.setApprovalStatus("202002");
                company.setOrgType("c");
                company.setCreateDate(new Date());
                this.save(company);
                //保存到用户表
                LambdaQueryWrapper<Company> qw = new LambdaQueryWrapper<>();
                qw.eq(Company::getPhone,companyDto.getPhone());
                Company one = this.getOne(qw);

                User user = new User();
                user.setCompanyId(one.getId());
                user.setCompanyName(companyDto.getName());
                user.setPhone(companyDto.getPhone());
                user.setPassword(DigestUtil.md5Hex("123456"));
                user.setUserpic(companyDto.getLogo());
                user.setUtype("platform");
                user.setIntro(companyDto.getIntro());
                user.setCreateDate(companyDto.getCreateDate());
                user.setStatus(CommonConstants.USING_FLAG);
                userMapper.insert(user);
                return ;
            }

        }
        throw new BusinessException(ErrorCode.DATAERROR);
    }

    //根据id查询机构信息
    @Override
    public ResponseResult<Company> findCompanyById(String undefined) {
        //undefined="1217339321501278215";
        String approvalStatus = "202002";
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Company::getApprovalStatus,approvalStatus);
        Company one = this.getOne(queryWrapper);
        return ResponseResult.okResult(one);
    }

    //机构审核
    @Override
    public ResponseResult savecompany(ApproveDto approveDto) {
        LambdaQueryWrapper<Company> qw = new LambdaQueryWrapper<>();
        qw.eq(Company::getPhone,approveDto.getPhone());
        Company one = this.getOne(qw);
        if (one!=null){
            one.setApprovalComment(approveDto.getApprovalComment());
            one.setApprovalStatus(approveDto.getApprovalStatus());
            this.updateById(one);
            ResponseResult.okResult(true);
        }
        throw new BusinessException(ErrorCode.NOTFOUND);
    }
}
